package hu.crs.game.handler;

import hu.crs.coordinate.Coordinate;
import hu.crs.game.HeroProperties;
import hu.crs.game.entity.creature.monster.Monster;
import hu.crs.game.field.Impassable;
import hu.crs.game.registry.MonsterRegister;
import hu.crs.game.state.StateHolder;
import hu.crs.game.statespace.GameMap;

public class GameFlowHandler implements StateHolder {
	private static final long serialVersionUID = -5436263897786829063L;

	GameMap gm;
	MonsterRegister mr;
	HeroProperties hp;
	
	public GameFlowHandler(GameMap gm, MonsterRegister mr, HeroProperties hp) {
		super();
		this.gm = gm;
		this.mr = mr;
		this.hp = hp;
	}
	
	public void register(int x, int y, Monster m) {
		register(new Coordinate(x, y), m);
	}
	
	public void register(Coordinate co, Monster m) {
		if (mr.add(m)) {
			gm.add(co, m);
		}		
	}
	
	public boolean validateMove(Coordinate p) {
		return !(gm.get(p).getField() instanceof Impassable);
	}
	
	public void updateMap(Coordinate newPos) {
		gm.get(hp.getPosition()).removeHero();
		hp.setPosition(newPos);
		gm.add(newPos, hp.getHero());
	}
	
	public void finishTurn(Coordinate c) {
		validateMove(c);
		updateMap(c);
		
	}

}
